package com.czie.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import static com.czie.myapplication.BroadcastSender.BROADCAST_ACTION;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    int i = 0;
    //id声明
    private ImageView lightView, light2View, alarmView, fanView, SmokeImageView, ShakeImageView, FireImageView, BodyImageView;
    private TextView lightTextView, light2TextView, alarmTextView, fanTextView;
    //MyBroadReceiver内部类
    MyBroadReceiver myBroadReceiver = new MyBroadReceiver();
    // 网关Id
    public static final String GATEWAYID = "00000008";
    //      分别对应 灯1，灯2
    public static final String LIGHTID = GATEWAYID + "0C010301";
    public static final String LIGHTID2 = GATEWAYID + "0C010302";
    public static final String ALARMID = GATEWAYID + "0C010303";
    public static final String FANID = GATEWAYID + "0C010304";

    // 创建SensorManager
    SensorManager sensorManager = SensorManager.getManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 设置串口服务器
        sensorManager.setSerialServerIp("192.168.1.100");
        // 启动服务
        sensorManager.startGatherService(this, GATEWAYID);
        // 注册广播接收器
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BROADCAST_ACTION);
        // 注册广播类
        registerReceiver(myBroadReceiver, intentFilter);
        //初始化界面
        initView();
    }

    private void initView() {
        lightView = findViewById(R.id.lightView);
        light2View = findViewById(R.id.light2View);
        alarmView = findViewById(R.id.alarmView);
        fanView = findViewById(R.id.fanView);
        lightView.setOnClickListener(this);
        light2View.setOnClickListener(this);
        alarmView.setOnClickListener(this);
        fanView.setOnClickListener(this);
        lightTextView = findViewById(R.id.lightTextView);
        light2TextView = findViewById(R.id.light2TextView);
        alarmTextView = findViewById(R.id.alarmTextView);
        fanTextView = findViewById(R.id.fanTextView);
    }

    public void init(ImageView imageView) {
        lightView.setSelected(false);
        alarmView.setSelected(false);
        fanView.setSelected(false);
        imageView.setSelected(true);
    }

    // 控制开关的方法
    public void switchClick(ImageView imageView, String senorId, TextView textView) {
        if (i < 1) {
            i++;
            try {
                sensorManager.switchControl(senorId, 1);
                textView.setText("开");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            i = 0;
            try {
                sensorManager.switchControl(senorId, 0);
                imageView.setSelected(false);
                textView.setText("关");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lightView:
                init(lightView);
                switchClick(lightView, LIGHTID, lightTextView);
                break;
            case R.id.light2View:
                init(light2View);
                switchClick(light2View, LIGHTID2, light2TextView);
                break;
            case R.id.alarmView:
                init(alarmView);
                switchClick(alarmView, ALARMID, alarmTextView);
                break;
            case R.id.fanView:
                init(fanView);
                switchClick(fanView, FANID, fanTextView);
                break;
            default:
                break;
        }
    }
}

// 广播接收器
class MyBroadReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 拿到数据，更新UI
        Bundle bundle = intent.getExtras();
        String sensorId = bundle.getString("sensorId");
        String data = bundle.getString("data");
        int sensorType = bundle.getInt("sensorType");
        Log.i("EEEEEE", "获取到的传感器数据：" + intent.getAction() + ":" + sensorId + "," + sensorType + "," + data);
        if (sensorType == Sensor.WET_SENSOR) {
//                String value =  sensorManager.getSensorValue(Sensor.WET_SENSOR);
            if (data != null) {
                //  helloTextView.setText(data);
            }
        }
    }

}